<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * Smarty i18n modifier plugin
 *
 * Type:     modifier<br>
 * Name:     i18n<br>
 * Purpose:  Extract a specific language translation from a tag encoded language chain<br>
 * Input:<br>
 *         - string: input language chain in format
 *		"<en>english</en><fr>anglais</fr>"
 *         - language_code : the language code to extract, if none specified, we will take
 *		the one of DRX_Multilanguage
 *         - use_default_on_fail : boolean. If TRUE, we will try to extract the default
 *		language translation in case of failure. Otherwise, we just return an
 *		empty string on failure.
 * @author   Clément Nodet <clement dot nodet at gmail dot com>
 * @param string
 * @param string
 * @return string|void
 * @uses smarty_make_timestamp()
 */
function smarty_modifier_i18n($string, $language_code = '', $use_default_on_fail = TRUE)
{
	if ($language_code == '')
	{
		$language_code = DRX_Multilanguage::getLanguage();
	}

	// Try to find the chain in the string
	if (preg_match('/<'.$language_code.'>([^<]*)<\/'.$language_code.'>/',$string,$matches) > 0)
	{
		return str_replace(array('&lt;','&gt;'),array('<','>'),$matches[1]);
	}
	else
	{
		// We couldn't find it, do we need to try again with the default language?
		if ($use_default_on_fail and $language_code != DEFAULT_LANGUAGE)
		{
			return smarty_modifier_i18n($string,DEFAULT_LANGUAGE,FALSE);
		}
		else
		{
			return '';
		}
	}
}

?>